const { adminsModel } = require('../models/adminModels');
const { salesModel } = require('../models/salesModels')
//管理员登录
module.exports.login = async (val) => {

  const data = await adminsModel.find(val)
  return data
};
//销售员登录
module.exports.salesLogin = async (val) => {
  const data = await salesModel.find(val)
  return data
}
// 获取管理员
module.exports.getAdmins = async function ({ currentPage = 1, pageSize = 3 }) {
  const total = await adminsModel.countDocuments({ delete: false });
  const data = await adminsModel.find({ delete: false }).limit(pageSize - 0).skip(pageSize * (currentPage - 1));
  return { data, total };
}
// 修改管理员权限
module.exports.updateState = async function ({ _id, state }) {
  const data = await adminsModel.updateOne({ _id }, { state });
  return data;
}
// 根据账号查询是否存在该管理员
module.exports.getAdminsByUsername = async function (data) {
  const result = await adminsModel.find({ delete: false, ...data });
  return result;
}
// 添加管理员
module.exports.addAdmins = async function (data) {
  const result = await adminsModel.create(data);
  return result;
}
// 删除管理员
module.exports.deleteAdmins = async function (data) {
  const result = await adminsModel.updateOne(data, { delete: true });
  return result;
}
// 根据管理员获取id
module.exports.getAdminsById = async function (data) {
  const result = await adminsModel.find(data);
  return result;
}
// 修改管理员信息
module.exports.updateAdmins = async function ({ _id, password }) {
  const result = await adminsModel.updateOne({ _id }, { password })
  return result;
}